package com.ss.android.b.a;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.utility.Logger;
import com.ss.android.b.bb;
import com.ss.android.common.ad.MobAdClickCombiner;
import com.ss.android.common.app.permission.PermissionsManager;
import com.ss.android.common.app.permission.PermissionsResultAction;
import com.ss.android.common.dialog.AlertDialog;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.util.ToolUtils;
import com.ss.android.common.util.UrlBuilder;
import com.ss.android.newmedia.activity.browser.BrowserActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.URLEncoder;
import java.util.List;

public class AdsAppItem {
    private static final String l = AdsAppItem.class.getSimpleName();
    public String packageName;
    public long id;
    public String openUrl;
    public String displayInfo;
    public String downloadUrl;
    public String webUrl;
    public String appName;
    public long displayDuration;
    public List trackUrlList;
    public String displayTemplate;
    public boolean k;
    private String m;

    private AdsAppItem(String arg1) {
        this.m = arg1;
    }

    public static void a(final Context arg25, String arg26, String arg27, final String arg28, String arg29, final long arg30, final String arg32, String arg33, String arg34, final String arg35, String arg36, String arg37, String arg38, String arg39) {
        if (arg25 != null) {
            final String v6 = android.support.design.a.isTextEmpty(arg33) ? "embeded_ad" : arg33;
            if (android.support.design.a.isTextEmpty(null)) {
                arg34 = "open";
            }

            JSONObject v4 = null;
            final JSONObject v12;
            JSONObject v1;
            try {
                if (!android.support.design.a.isTextEmpty(arg39)) {
                    v4 = new JSONObject();
                    v4.put("log_extra", arg39);
                }
                v1 = v4;
            } catch (Exception v4_1) {
                v1 = null;
            }


            v12 = v1;
            MobClickCombiner.onEvent(arg25, v6, "click", arg30, 0, v12);
            if (com.ss.android.b.bb.a().b(arg25, arg26, arg27)) {
                MobClickCombiner.onEvent(arg25, v6, arg34, arg30, 0, v12);
                return;
            }

            if (!android.support.design.a.isTextEmpty(arg28)) {
                if (!android.support.design.a.isTextEmpty(arg38)
                        && (!android.support.design.a.isTextEmpty(arg36)
                        || !android.support.design.a.isTextEmpty(arg37))) {
                    a(arg25, arg38, arg36, arg37, new AdsAppItem.a$a() {

                        public final void a() {
                            final JSONObject v8 = new JSONObject();
                            JSONObject v0 = new JSONObject();
                            try {
                                v0.put("url", arg28);
                                v0.put("ad_id", arg30);
                                v8.put("label", v6);
                                v8.put("ext_json", v0);
                            } catch (JSONException v0_1) {
                            }

                            if (Build.VERSION.SDK_INT < 23) {
                                bb.a().a(arg28, arg32, arg25, true, true, true, false, v8);
                            } else if ((arg25 instanceof Activity)) {
                                PermissionsManager.getInstance().requestPermission(
                                        (Activity) arg25,
                                        new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"},
                                        new PermissionsResultAction() {

                                            public final void onGranted() {
                                                bb.a().a(arg28, arg32, arg25, true, true, true, false, v8);
                                            }

                                            public final void onDenied(String arg1) {
                                            }
                                        });
                            }

                            MobClickCombiner.onEvent(arg25, v6, arg35, arg30, 0, v12);
                        }

                        public final void b() {
                            MobClickCombiner.onEvent(arg25, v6, "download_cancel", arg30, 0, v12);
                        }

                        public final void c() {
                            MobClickCombiner.onEvent(arg25, v6, "download_cancel", arg30, 0, v12);
                        }
                    });
                    return;
                }

                final JSONObject v21 = new JSONObject();
                v4 = new JSONObject();
                try {
                    v4.put("url", arg28);
                    v4.put("ad_id", arg30);
                    v21.put("label", v6);
                    v21.put("ext_json", v4);
                } catch (JSONException v4_2) {
                }

                if (Build.VERSION.SDK_INT < 23) {
                    com.ss.android.b.bb.a().a(arg28, arg32, arg25, true, true, true, false, v21);
                } else if ((arg25 instanceof Activity)) {
                    PermissionsManager.getInstance().requestPermission((Activity) arg25,
                            new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"},
                            new PermissionsResultAction() {

                                public final void onGranted() {
                                    com.ss.android.b.bb.a().a(arg28, arg32, arg25, true, true, true, false, v21);
                                }

                                public final void onDenied(String arg1) {
                                }
                            });
                }

                MobClickCombiner.onEvent(arg25, v6, arg35, arg30, 0, v12);
                return;
            }

            if (!android.support.design.a.isTextEmpty(arg29)) {
                a(arg25, arg26, arg29, arg32, 0, new b(arg25, v6, "click", arg30, arg39));
            }
        }
    }

    public static void a(Context arg6, String arg7, String arg8, String arg9, int arg10, b arg11) {
        if (arg6 != null) {
            if (!android.support.design.a.isTextEmpty(arg7)) {
                a(arg7, arg8);
                if (!a(arg6, arg7, arg11)) {
                } else {
                    return;
                }
            }

            if (android.support.design.a.isTextEmpty(arg8)) {
                return;
            }

            if (android.support.design.a.isTextEmpty(arg8)) {
                return;
            }

            long v0 = arg11.c;
            String v2 = arg11.d;
            try {
                if (TTUtils.isValidUrl(arg8)) {
                    Intent v3 = new Intent(arg6, BrowserActivity.class);
                    v3.putExtra("use_swipe", true);
                    v3.putExtra("ad_id", v0);
                    if (!android.support.design.a.isTextEmpty(v2)) {
                        v3.putExtra("bundle_download_app_log_extra", v2);
                    }

                    if (!android.support.design.a.isTextEmpty(arg9)) {
                        v3.putExtra("title", arg9);
                    }

                    v3.putExtra("orientation", arg10);
                    v3.setData(Uri.parse(arg8));
                    arg6.startActivity(v3);
                } else {
                    Intent v1 = new Intent("android.intent.action.VIEW", Uri.parse(arg8));
                    if (!android.support.design.a.isTextEmpty(v2)) {
                        v1.putExtra("bundle_download_app_log_extra", v2);
                    }

                    arg6.startActivity(v1);
                }

                arg11.a();
            } catch (Exception v0_1) {
                v0_1.printStackTrace();
            }
        }
    }

    public static void a(Context arg4, String arg5, String arg6, String arg7, final AdsAppItem.a$a arg8) {
        if (android.support.design.a.isTextEmpty(arg5)) {
            if (android.support.design.a.isTextEmpty(arg6)) {
                arg6 = arg4.getString(com.ss.android.article.browser.R.string.adsappitem_download_this_app);
            }

            arg5 = String.format(arg4.getString(com.ss.android.article.browser.R.string.adsappitem_download_info), arg6, arg7);
        }

        AlertDialog.Builder v0 = com.ss.android.j.b.createDialogBuilder(arg4);
        v0.setMessage(arg5)
                .setPositiveButton(com.ss.android.article.browser.R.string.label_ok,
                        new DialogInterface.OnClickListener() {

                            public final void onClick(DialogInterface arg2, int arg3) {
                                arg8.a();
                            }
                        })
                .setOnCancelListener(new DialogInterface.OnCancelListener() {

                    public final void onCancel(DialogInterface arg2) {
                        arg8.c();
                    }
                });
        v0.setNegativeButton(com.ss.android.article.browser.R.string.label_cancel, new DialogInterface.OnClickListener() {
            public final void onClick(DialogInterface arg2, int arg3) {
                arg8.b();
            }
        });
        v0.show();
    }

    public static AdsAppItem fromJO(JSONObject jo) throws JSONException {
        AdsAppItem item;
        String type = jo.getString("type");
        if (("app".equals(type)) || ("web".equals(type))) {
            item = new AdsAppItem(type);
            item.displayInfo = jo.optString("display_info");
            item.displayTemplate = jo.optString("display_template");
            item.openUrl = jo.optString("open_url");
            item.downloadUrl = jo.optString("download_url");
            item.webUrl = jo.optString("web_url");
            item.appName = jo.optString("app_name");
            item.packageName = jo.optString("package_name");
            item.displayDuration = jo.optLong("display_duration");
            item.id = jo.optLong("id");
            item.trackUrlList = BaseAdsData.a(jo, new String[1]);
        } else {
            item = null;
        }

        return item;
    }

    static void a(Context arg13, String arg14, String arg15, long arg16, String logExtra, int arg19) {
        JSONObject logExtraJO;
        if (arg13 != null) {
            String v4 = android.support.design.a.isTextEmpty(arg14) ? "embeded_ad" : arg14;
            String v5 = android.support.design.a.isTextEmpty(arg15) ? "click" : arg15;
            try {
                JSONObject logExtraJOO = null;
                if (!android.support.design.a.isTextEmpty(logExtra)) {
                    logExtraJOO = new JSONObject();
                    logExtraJOO.put("log_extra", logExtra);
                }
                logExtraJO = logExtraJOO;
            } catch (Exception v2_1) {
                logExtraJO = null;
            }
            MobAdClickCombiner.onAdEvent(arg13, v4, v5, arg16, 0, logExtraJO, arg19);
        }
    }

    private static boolean a(String arg3, String arg4) {
        if (!android.support.design.a.isTextEmpty(arg3)) {
            if (!arg3.startsWith("jdsdk://")) {
                if (arg3.startsWith("taobaosdk://")) {
                } else {
                    return false;
                }
            }

            try {
                if (android.support.design.a.isTextEmpty(Uri.parse(arg4).getHost())) {
                    return false;
                }

                com.ss.android.b.aa.a();
                com.ss.android.b.aa.b();
            } catch (Exception v0) {
                Logger.d(l, v0.toString());
            }
        }

        return false;
    }

    private static boolean a(Context arg8, String arg9, b arg10) {
        boolean v0 = false;
        if (!android.support.design.a.isTextEmpty(arg9)) {
            long v2 = 0;
            String v1 = "";
            if (arg10 != null) {
                v2 = arg10.c;
                v1 = arg10.d;
            }

            try {
                if (arg9.contains("__back_url__")) {
                    UrlBuilder v4 = new UrlBuilder(com.ss.android.b.aa.b);
                    v4.addParam("adId", v2);
                    v4.addParam("log_extra", v1);
                    arg9 = arg9.replace("__back_url__", URLEncoder.encode(v4.build(), "UTF-8"));
                }

                Uri v2_1 = Uri.parse(arg9);
                Intent v3 = new Intent("android.intent.action.VIEW");
                v3.setData(v2_1);
                if (ToolUtils.isInstalledApp(arg8, v3)) {
                    v3.putExtra("open_url", arg9);
                    if (!android.support.design.a.isTextEmpty(v1)) {
                        v3.putExtra("bundle_download_app_log_extra", v1);
                    }

                    arg8.startActivity(v3);
                    Logger.d(l, "open url call sdk success");
                    if (arg10 != null) {
                        arg10.a();
                        a(arg10.b, arg10.a, "open_url_app", arg10.c, arg10.d, arg10.e);
                    }

                    return true;
                }

                if (arg10 == null) {
                    return v0;
                }

                arg10.a();
                a(arg10.b, arg10.a, "open_url_h5", arg10.c, arg10.d, arg10.e);
            } catch (Exception v1_1) {
                if (arg10 == null) {
                    return v0;
                }

                a(arg10.b, arg10.a, "open_url_abnormal", arg10.c, arg10.d, arg10.e);
            }
        }

        return v0;
    }

    public final boolean a(final Context context) {
        int v0 = 0;
        boolean v1 = true;
        if (context == null) {
            return false;
        }

        Intent intent;
        if ("web".equals(this.m)) {
            try {
                if (android.support.design.a.isTextEmpty(this.webUrl)) {
                    return v1;
                }
                intent = new Intent(context, BrowserActivity.class);
                intent.setData(Uri.parse(this.webUrl));
                context.startActivity(intent);
            } catch (Exception exception) {
            }

            return v1;
        }

        AlertDialog.Builder builder = com.ss.android.j.b.createDialogBuilder(context);
        builder.setMessage(String.format(context.getString(com.ss.android.article.browser.R.string.adsapp_tip_app), this.appName));
        if (!android.support.design.a.isTextEmpty(this.webUrl)) {
            DialogInterface.OnClickListener v0_3 = new DialogInterface.OnClickListener() {

                public final void onClick(DialogInterface arg9, int arg10) {
                    long v6 = 0;
                    try {
                        if (TTUtils.isValidUrl(AdsAppItem.this.webUrl)) {
                            Intent intent = new Intent(context, BrowserActivity.class);
                            intent.setData(Uri.parse(AdsAppItem.this.webUrl));
                            context.startActivity(intent);
                        } else {
                            context.startActivity(new Intent("android.intent.action.VIEW", Uri.parse(AdsAppItem.this.webUrl)));
                        }
                    } catch (Exception v0) {
                    }

                    if (AdsAppItem.this.id > v6) {
                        MobClickCombiner.onEvent(context, "notify", "tips_alert_preview", AdsAppItem.this.id, v6);
                    }
                }
            };
            builder.alertParams.mNeutralButtonText = builder.alertParams.mContext.getText(com.ss.android.article.browser.R.string.adsapp_button_web);
            builder.alertParams.mNeutralButtonListener = v0_3;
            v0 = 1;
        }

        if (!android.support.design.a.isTextEmpty(this.downloadUrl)) {
            builder.setPositiveButton(com.ss.android.article.browser.R.string.adsapp_button_download, new DialogInterface.OnClickListener() {
                public final void onClick(DialogInterface arg13, int arg14) {
                    long v10 = 0;
                    final JSONObject v8 = new JSONObject();
                    JSONObject v0 = new JSONObject();
                    try {
                        v0.put("url", AdsAppItem.this.downloadUrl);
                        v0.put("ad_id", AdsAppItem.this.id);
                        v8.put("label", "embeded_ad");
                        v8.put("ext_json", v0);
                    } catch (JSONException v0_1) {
                    }

                    if (Build.VERSION.SDK_INT < 23) {
                        bb.a().a(AdsAppItem.this.downloadUrl,
                                AdsAppItem.this.appName,
                                context,
                                true,
                                true, true, false, v8);
                    } else if ((context instanceof Activity)) {
                        PermissionsManager.getInstance().requestPermission(
                                (Activity) context,
                                new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"},
                                new PermissionsResultAction() {

                                    public final void onGranted() {
                                        bb.a().a(AdsAppItem.this.downloadUrl, AdsAppItem.this.appName, context, true, true, true, false, v8);
                                    }

                                    public final void onDenied(String arg1) {
                                    }
                                });
                    }

                    if (AdsAppItem.this.id > v10) {
                        MobClickCombiner.onEvent(context, "notify", "tips_alert_install", AdsAppItem.this.id, v10);
                    }
                }
            });
            v0 = 1;
        }

        if (v0 != 0) {
            builder.setNegativeButton(com.ss.android.article.browser.R.string.adsapp_button_cancel, new DialogInterface.OnClickListener() {

                public final void onClick(DialogInterface arg9, int arg10) {
                    long v6 = 0;
                    if (AdsAppItem.this.id > v6) {
                        MobClickCombiner.onEvent(context, "notify", "tips_alert_cancel", AdsAppItem.this.id, v6);
                    }
                }
            });
            builder.show();
            return v1;
        }

        try {
            if (android.support.design.a.isTextEmpty(this.openUrl)) {
                return v1;
            }

            if (TTUtils.isValidUrl(this.openUrl)) {
                intent = new Intent(context, BrowserActivity.class);
                intent.setData(Uri.parse(this.openUrl));
                context.startActivity(intent);
                return v1;
            }

            context.startActivity(new Intent("android.intent.action.VIEW", Uri.parse(this.openUrl)));
        } catch (Exception v0_1) {
        }

        return v1;
    }

    public interface a$a {
        void a();

        void b();

        void c();
    }

    public static final class b {
        public String a;
        public Context b;
        public long c;
        public String d;
        public int e;
        private String f;

        public b(Context arg3, String arg4, String arg5, long arg6, String arg8) {

            this.e = 0;
            this.a = arg4;
            this.b = arg3;
            this.c = arg6;
            this.d = arg8;
            this.f = arg5;
        }

        public b(Context arg3, String arg4, String arg5, long arg6, String arg8, int arg9) {

            this.e = 0;
            this.a = arg4;
            this.b = arg3;
            this.c = arg6;
            this.d = arg8;
            this.f = arg5;
            this.e = arg9;
        }

        public final void a() {
            if (!android.support.design.a.isTextEmpty(this.f)) {
                AdsAppItem.a(this.b, this.a, this.f, this.c, this.d, this.e);
            }
        }
    }
}

